//
//  LKOpenSubordinateDataViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/19.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKOpenSubordinateDataViewController.h"
#import "LKOpenSubordinateViewController.h"
#import "LKOpenSubordinateRateSettingViewController.h"
#import "LKMySuboridateOpenInforModel.h"
#import "LKUFPictureRow.h"
#import "LKUFPictureRowCell.h"
@interface LKOpenSubordinateDataViewController ()


@property(nonatomic,strong)UFFormView *formview;
@property(nonatomic,strong)UFSection *headerSection;
@property(nonatomic,strong)UFSection *secondHeaderSection;
@property(nonatomic,copy)NSString *hadSetting;
@property(nonatomic,copy)NSString *pictruehadSetting;
@property(nonatomic,strong)LKMySuboridateOpenInforModel *inforModel;
@end

@implementation LKOpenSubordinateDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"代理资料";
    if (self.uid) {
        [self getMyAgentInformationData];
    }

    self.hadSetting = @"";
    [self setNavgationRightBtn];
    [self createFormView];
    // Do any additional setup after loading the view.
}


-(void)setNavgationRightBtn{
//    WS(weakSelf)
//    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"编辑" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
//        LKOpenSubordinateViewController *vc = [[LKOpenSubordinateViewController alloc] init];
//        vc.inforModel = weakSelf.inforModel;
//        vc.isEdit = YES;
//        [weakSelf.navigationController pushViewController:vc animated:YES];
//    }];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

/// 创建表单的view
-(void)createFormView{
    WS(weakSelf)
    self.formview = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .registerRow([LKUFPictureRow class], [LKUFPictureRowCell class])
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .width(WIDTH/3 + 50)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"代理费率")
                .value(weakSelf.hadSetting)
                .rowDidSelected(^(__kindof UFRow * _Nonnull row) {
                    LKOpenSubordinateRateSettingViewController *ratevc = [[LKOpenSubordinateRateSettingViewController alloc] init];
                    ratevc.isCheckStatu = YES;
                    ratevc.modelBlock = ^(LKMySuboridateOpenInforModel * _Nullable model) {
                        weakSelf.inforModel = model;
                        weakSelf.hadSetting = @"已设置";
                        [weakSelf.formview removeFromSuperview];
                        [weakSelf createFormView];
                    };
                    ratevc.inforModel = weakSelf.inforModel;
                    [weakSelf.navigationController pushViewController:ratevc animated:YES];
                })
                .accessoryType(UFRowAccessoryDisclosureIndicator);
            }]);
            
        }])
        .addToSuperView(self.view);
    }];

    [self addFirstSectionHeader];
    [self.formview addSection:self.headerSection];
    [self addSecondSectionHeader];
    [self.formview addSection:self.secondHeaderSection];
        if (self.showBottomBtn) {
//            [self.formview mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.right.equalTo(self.view);
//                make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT);
//                make.bottom.equalTo(self.view).offset(0);
//            }];
            UIView *bottomView = [[UIView alloc] initWithFrame:CGRectZero];
            [self.view addSubview:bottomView];
            [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.view);
                make.height.mas_equalTo(50);
            }];
            LKBaseButton *deleteBtn = [LKBaseButton buttonWithFrame:CGRectZero type:UIButtonTypeCustom title:@"删除" titleColor:[UIColor whiteColor] layercorne:0 font:[UIFont fontWithName:@"PingFangSC-Regular" size:20] backgroudColor:[UIColor redColor] AndActionBlock:^(UIButton * _Nonnull btn) {
                NSDictionary *param = @{@"id":weakSelf.inforModel.Id};
                [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MySuboradinateCheckDeleteUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
                    
                } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                    if ([responseObject[@"code"] isEqualToString:@"success"]) {
                     [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"mangerMyOpenSubordinateReload" object:nil];
                    }else{
                        [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                    }
                    
                } failure:^(NSString * _Nullable errorMessage) {
                    
                }];
            }];
            [bottomView addSubview:deleteBtn];
            LKBaseButton *refuseBtn = [LKBaseButton buttonWithFrame:CGRectZero type:UIButtonTypeCustom title:@"审核不通过" titleColor:[UIColor whiteColor] layercorne:0 font:[UIFont fontWithName:@"PingFangSC-Regular" size:20] backgroudColor:[UIColor redColor] AndActionBlock:^(UIButton * _Nonnull btn) {

                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请填写审核不通过理由" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                
                    }];
                    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                    }]];
                    [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        UITextField *contentTf = alert.textFields.firstObject;
                        NSDictionary *param = [weakSelf.inforModel properties_aps];
                        NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithDictionary:param];
                        [mutableDic setObject:weakSelf.inforModel.Id forKey:@"agent_id"];
                        [mutableDic setObject:contentTf.text forKey:@"msg"];
                        [mutableDic setObject:@"2" forKey:@"status"];
                        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MySuboradinateCheckEditUrl header:[LKUserInforTool account].token parameters:mutableDic progress:^(NSProgress * _Nullable progress) {
                            
                        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                            if ([responseObject[@"code"] isEqualToString:@"success"]) {
                             [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                                [weakSelf.navigationController popViewControllerAnimated:YES];
                            }else{
                                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                            }
                        } failure:^(NSString * _Nullable errorMessage) {
                            
                        }];
                    }]];
                    [self presentViewController:alert animated:YES completion:nil];

            }];
            [bottomView addSubview:refuseBtn];
            LKBaseButton *agreeBtn = [LKBaseButton buttonWithFrame:CGRectZero type:UIButtonTypeCustom title:@"审核通过" titleColor:[UIColor whiteColor] layercorne:0 font:[UIFont fontWithName:@"PingFangSC-Regular" size:20] backgroudColor:[UIColor greenColor] AndActionBlock:^(UIButton * _Nonnull btn) {
                NSDictionary *param = @{};
                [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MySuboradinateCheckEditUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
                    
                } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请填写审核通过理由" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                    
                        }];
                        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                        }]];
                        [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            UITextField *contentTf = alert.textFields.firstObject;
                            NSDictionary *param = [weakSelf.inforModel properties_aps];
                            NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithDictionary:param];
                            [mutableDic setObject:weakSelf.inforModel.Id forKey:@"agent_id"];
                            [mutableDic setObject:contentTf.text forKey:@"msg"];
                            [mutableDic setObject:@"1" forKey:@"status"];
                            [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MySuboradinateCheckEditUrl header:[LKUserInforTool account].token parameters:mutableDic progress:^(NSProgress * _Nullable progress) {
                                
                            } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                                if ([responseObject[@"code"] isEqualToString:@"success"]) {
                                 [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                                    [weakSelf.navigationController popViewControllerAnimated:YES];
                                }else{
                                    [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                                }
                            } failure:^(NSString * _Nullable errorMessage) {
                                
                            }];
                        }]];
                        [self presentViewController:alert animated:YES completion:nil];
                } failure:^(NSString * _Nullable errorMessage) {
                    
                }];
            }];
            [bottomView addSubview:agreeBtn];
            NSArray *btsArray = @[refuseBtn,agreeBtn];
            [btsArray mas_distributeViewsAlongAxis:0 withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
            [btsArray mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(50);
                make.bottom.equalTo(bottomView.mas_bottom);
            }];
            if ([weakSelf.inforModel.agent_top_id isEqualToString:@"170"] && [[LKUserInforTool account].uid isEqualToString:@"591"]) {
                [self.view bringSubviewToFront:bottomView];
                refuseBtn.hidden = NO;
                agreeBtn.hidden = NO;
                bottomView.hidden = NO;
                [self.formview mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.right.equalTo(self.view);
                    make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT);
                    make.bottom.equalTo(self.view).offset(-50);
                }];
            }else{
                refuseBtn.hidden = YES;
                agreeBtn.hidden = YES;
                bottomView.hidden = YES;
                [bottomView removeFromSuperview];            }
            [self.formview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self.view);
                make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT);
                make.bottom.equalTo(self.view);
            }];
        }else{
            [self.formview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self.view);
                make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT);
                make.bottom.equalTo(self.view);
            }];
        }
}
/// 添加提交人信息部分
-(void)addFirstSectionHeader{
    WS(weakSelf)
    self.headerSection = [UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
        make
        .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
           make
            .width(WIDTH/3 + 50)
            .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
            .color(labelBlackColor)
            .textAlignment(NSTextAlignmentLeft);
        }])
        .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
            make
            .textAlignment(NSTextAlignmentRight)
            .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
            .color(labelBlackColor);
        }])
        .headerStyle([UFSectionStyle makeSectionStyle:^(UFSectionStyleMaker * _Nonnull make) {
             make
             .height(50)                         // 设置头高度
             .text(@"提交方信息")                      // 设置头描述信息
            .textAlignment(NSTextAlignmentCenter)
             .color(COLOR_HEX(0x333333))    // 设置头描述信息颜色
             .font([UIFont fontWithName:@"PingFangSC-Regular" size:16]); // 设置头描述信息字体
         }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .placeholder(@"")
            .height(RowHeightDefault)
            .title(@"代理名称")
            .value(weakSelf.inforModel.sub_name);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .placeholder(@"")
            .title(@"代理级别")
            .value(weakSelf.inforModel.sub_type)
            .height(RowHeightDefault);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .title(@"提交角色")
            .value([weakSelf.inforModel.juese integerValue] == 1 ? @"员工":@"代理")
            .height(RowHeightDefault);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .title(@"姓名")
            .value(weakSelf.inforModel.sub_uname)
            .height(RowHeightDefault);
        }]);

    }];
}
/// 添加基础信息部分
-(void)addSecondSectionHeader{
    WS(weakSelf)
    self.secondHeaderSection = [UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
        make
        .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
           make
            .width(WIDTH/3 + 50)
            .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
            .color(labelBlackColor)
            .textAlignment(NSTextAlignmentLeft);
        }])
        .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
            make
            .textAlignment(NSTextAlignmentRight)
            .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
            .color(labelBlackColor);
        }])
        .headerStyle([UFSectionStyle makeSectionStyle:^(UFSectionStyleMaker * _Nonnull make) {
             make
             .height(50)                         // 设置头高度
             .text(@"基础信息")                      // 设置头描述信息
            .textAlignment(NSTextAlignmentCenter)
             .color(COLOR_HEX(0x333333))    // 设置头描述信息颜色
             .font([UIFont fontWithName:@"PingFangSC-Regular" size:16]); // 设置头描述信息字体
         }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .placeholder(@"")
            .height(RowHeightDefault)
            .title(@"代理ID")
            .value(weakSelf.inforModel.Id);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .placeholder(@"")
            .title(@"代理全称")
            .value(weakSelf.inforModel.agent_name)
            .height(RowHeightDefault);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .title(@"代理简称")
            .value(weakSelf.inforModel.agent_jiancheng)
            .height(RowHeightDefault);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .title(@"用户手机号")
            .value(weakSelf.inforModel.user_phone)
            .height(RowHeightDefault);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .title(@"代理级别")
            .value(weakSelf.inforModel.agent_type_name)
            .height(RowHeightDefault);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .title(@"代理类型")
            .value(weakSelf.inforModel.level_agent_mode)
            .height(RowHeightDefault);
        }])
        .addRowWithBlock(^__kindof UFRow * _Nonnull{
            LKUFPictureRow *pictureRow = [[LKUFPictureRow alloc] init];
            pictureRow.headerTitle = @"资料照片";
            pictureRow.pictureNumbers = 8;
            pictureRow.picturesArray = [[NSMutableArray alloc] initWithArray:                @[[NSString judegeIsNullAndTransform:weakSelf.inforModel.id_card_img],[NSString judegeIsNullAndTransform:weakSelf.inforModel.back_id_card_img],[NSString judegeIsNullAndTransform:weakSelf.inforModel.hand_id_card_img],[NSString judegeIsNullAndTransform:weakSelf.inforModel.bussiness_img],[NSString judegeIsNullAndTransform:weakSelf.inforModel.agreement_img1],[NSString judegeIsNullAndTransform:weakSelf.inforModel.agreement_img2],[NSString judegeIsNullAndTransform:weakSelf.inforModel.agreement_img3],[NSString judegeIsNullAndTransform:weakSelf.inforModel.agreement_img4]]];;
            pictureRow.titlesPlaceArray = @[@"+身份证正面照",@"+身份证背面照",@"+手持身份证正面照",@"+营业执照",@"+合同第一页",@"+合同第二页",@"+合同第三页",@"+合同第四页"];
            pictureRow.height = 450;
            return pictureRow;
        })
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .title(@"所在地址")
            .value([NSString stringWithFormat:@"%@%@%@%@",weakSelf.inforModel.province,weakSelf.inforModel.city,weakSelf.inforModel.county,weakSelf.inforModel.address])
            .height(RowHeightDefault);
        }])
        .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
            make
            .editable(NO)
            .title(@"推荐人")
            .value(weakSelf.inforModel.referrer)
            .height(RowHeightDefault);
        }]);
        

    }];
}

/// 获取代理的详情数据
-(void)getMyAgentInformationData{
    NSDictionary *param = @{@"id":self.uid,@"token":[LKUserInforTool account].token};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodGET serverUrl:ServerlUrl apiPath:MySuboradinateCheckUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            self.inforModel = [LKMySuboridateOpenInforModel modelWithDictionary:responseObject[@"data"]];
            if (self.inforModel.ali_rate.length != 0 ) {
                self.hadSetting = @"已设置";
            }
            if (self.inforModel.id_card_img.length != 0 || self.inforModel.back_id_card_img.length != 0 || self.inforModel.hand_id_card_img.length != 0 || self.inforModel.bussiness_img.length != 0 || self.inforModel.agreement_img1.length != 0 || self.inforModel.agreement_img2.length != 0 || self.inforModel.agreement_img3.length != 0 || self.inforModel.agreement_img4.length != 0) {
                self.pictruehadSetting = @"已设置";
            }
            [self.formview removeFromSuperview];
            [self createFormView];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
        
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
